﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.Infrastructure.Net;
using WoChu.Infrastructure.Web;

namespace WoChu.Infrastructure.Video
{
    public class VideoCheck
    {
        /// <summary>
        /// 根据审核位，获取那个监控探头的ip地址，ip和审核位通过config获取
        /// </summary>
        /// <param name="checkLocation"></param>
        /// <returns></returns>
        public static VideoServer GetVideoServer(string checkLocation)
        {
            string[] ipList = ConfigSetting.GetConfigValueByName<string>("Video.CheckIP").Split(','); //
            string[] posNums = ConfigSetting.GetConfigValueByName<string>("Video.CheckLocation").Split(',');//{ "A", "B", "C" };

            int channelMax = 32;
            int posNumSize = 14;
            string posNum = checkLocation.Substring(0, 1); //第一个字符
            int posIndex = Convert.ToInt32(checkLocation.Substring(1)); //第2个字符
            int channelIndex = (Array.IndexOf(posNums, posNum)) * posNumSize + posIndex;
            string ip = ipList[channelIndex / channelMax];
            channelIndex = channelIndex % channelMax;
            return new VideoServer() { Ip = ip, ChannelIndex = channelIndex };
        }

        public static string DownLoad(VideoHelper vedio, VideoServer server, DateTime dtStart, DateTime dtEnd, string checkLocation)
        {
            string videoPath = GetVideoPath(dtStart, dtEnd, checkLocation);
            vedio.DownLoadVideoFile(dtStart, dtEnd, server.ChannelIndex, videoPath);
            return videoPath;
        }

        public static string GetVideoPath(DateTime dtStart, DateTime dtEnd, string checkLocation)
        {
            string vedioFile = string.Format("{0}_{1:yyyyMMddHHmmss}_{2:yyyyMMddHHmmss}.mp4", checkLocation, dtStart, dtEnd);
            string savePath = ConfigSetting.GetConfigValueByName<string>("Video.SavePath");
            savePath = DirectoryHelper.GetDirNoCreate(savePath);
            string videoPath = DirectoryHelper.PathCombine(savePath, vedioFile);
            return videoPath;
        }

        public static string GetVideoPath(string fileName)
        {
            string savePath = ConfigSetting.GetConfigValueByName<string>("Video.SavePath");
            savePath = DirectoryHelper.GetDirNoCreate(savePath);
            string videoPath = string.Format(@"{0}{1}", savePath, fileName);
            return videoPath;
        }
    }
}
